package org.cnbi.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.cnbi.entity.Dashboard;
import org.cnbi.entity.Menu;
import org.cnbi.service.IHandleService;
import org.cnbi.utils.chart.ChartParamter;
import org.cnbi.utils.chart.json.JsonSeriesChart;
import org.cnbi.utils.tools.ActionConstants;
import org.cnbi.utils.tools.MenuConstants;
import org.cnbi.web.controller.base.BaseController;
import org.cnbi.web.view.xml.ChartUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 系统请求页面的核心控制器 凡是是请求页面的动作，都到这里来写吧！ 
 * Copyright © 2014中国经邦. All rights reserved.
 * @author 龚佳新
 * @date 2014年4月17日下午12:47:04
 * @cnbi-web
 */
@Controller
@RequestMapping("/sys")
public class SystemController extends BaseController {
	
	
	@Autowired
	private IHandleService handleService;
	
	@RequestMapping(value = "/main")
	public ModelAndView main(HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		String scode = getRequest().getParameter("scode");
		commonHandle(scode,view, response);
		if(scode.equals(MenuConstants.DEFALUT)){
			defaults(view);
		}else if(scode.equals(MenuConstants.DASH)){
			dash(view);
		}else if(scode.equals(MenuConstants.INDICATOR)){
			indicator(view);
		}else if(scode.equals(MenuConstants.TABLE)){
			
		}else if(scode.equals(MenuConstants.REPORT)){
			
		}else if(scode.equals(ActionConstants.MANAGE)){
			
		}
		return view;
	}
	public void defaults(ModelAndView view){
		
	}
   public void indicator(ModelAndView view){
		
	}
	
	
	public void dash(ModelAndView view) {
		List<Dashboard> list = new ArrayList<Dashboard>();
		list.add(new Dashboard("profita","利润预测模型","dash","这个模型反映了可以对企业利润收入情况进行提前预测……"));
		list.add(new Dashboard("assets","资产预测模型","dash","这个模型可对企业整个资产进行分析"));
		list.add(new Dashboard("debt","偿债预测模型","dash","这个模型可对企业整个资产进行分析，以规避不必要的风险……"));
		list.add(new Dashboard("cash_flow","现金预测流量","dash","这个模型可对企业整个资产进行分析，以规避不必要的风险……"));
		list.add(new Dashboard("cbfy","成本费用模型","dash","这个模型可对企业整个资产进行分析，以规避不必要的风险……"));
		list.add(new Dashboard("dupont","杜邦预测模型","dash","这个模型可对企业整个销售，财务等环节的费用进行预测"));
		list.add(new Dashboard("eva","EVA预测模型","dash","这个模型可对企业整个资产进行分析，以规避不必要的风险……"));
		view.addObject("listDatas", list);//imageSubfix
	}

	


	public void report(ModelAndView view){
		
	}
	
	public ModelAndView userM(HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		commonHandle(MenuConstants.USERM,view, response);
		return view;
	}


	public ModelAndView tableM(HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		commonHandle(MenuConstants.TABLEM,view, response);
		return view;
	}
	
	
	public void manage(ModelAndView view) {
		List<Menu> list = new ArrayList<Menu>();
		list.add(new Menu("userM","用户管理","manage","可对用户进行增删改查等操作,还可查看在线用户"));
		list.add(new Menu("dataM","数据管理","manage","可对数据进行增删改查等操作,亦实现U8,KS数据接口导入"));
		list.add(new Menu("rightM","权限管理","manage","可对用户进行菜单，角色，其它功能进行授权操作"));
		list.add(new Menu("tableM","报表管理","manage","目前应用了财务的3张主表，可操作哦"));
		list.add(new Menu("reportM","报告管理","manage","按公司，期间维度分的报告"));
		list.add(new Menu("dashM","模型管理","manage","目前应用了财务主表预测模型"));
		list.add(new Menu("newsM","新闻管理","manage","目前应用了财务主表预测模型"));
		list.add(new Menu("sysM","系统设置","manage","对系统相关参数进行设置"));
		view.addObject("listDatas", list);
		view.addObject("imageSubfix", ".png");
	}

	/**
	 * jndi测试数据源页面的请求
	 * 
	 * @return
	 */
	@RequestMapping("/jndi")
	public String jndi() {
		return "jndi";
	}

	/**
	 * sql查询数据页面
	 * 
	 * @param sqlParamBean
	 * @return
	 */
	/*@RequestMapping("/query")
	// @RequestParam("sql")String sql
	public ModelAndView query(SqlParamsBean sqlParamBean) {// period
		ModelAndView view = new ModelAndView("query");
		String subject = sqlParamBean.getSubject();
		String sqlKey = "0001", sql = "";
		if (null != subject) {
			sqlKey = subject;
		}
		sql = getSubjectSqlBeanByKey(sqlKey);
		view.addObject("sql", sql);
		List<Map> list = (List<Map>) handleService.queryListMapBean(sql, sqlParamBean);
		view.addObject(Constants.DATAS, list);
		return view;
	}*/

	/**
	 * 注意 只有 GET方法支持 URL带参数的功能
	 * @return
	 */
	@RequestMapping("/view")
	@ResponseBody
	public Object view(ChartParamter page, HttpServletResponse response) {
		String type = page.getType();
		JsonSeriesChart chart = ChartUtil.createJsonSeriesChart();
		if (type.equals("xml")) {
			return ChartUtil.createXmlSeriesChart();
		}
		return chart;
	}
	
	/**
     * 注意 只有 GET方法支持 URL带参数的功能 
     * http://192.168.1.118:8087/cnbi-web/main/view1/paramName
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value ="view1/{type}"/*,produces="text/plain;charset=UTF-8"*/)
    @ResponseBody
    public Object getChart(@PathVariable String type,HttpServletResponse response){ 	    	
    	System.out.println("type=="+type);
    	 JsonSeriesChart chart = ChartUtil.createJsonSeriesChart(); 
    	  if(type.equals("xml")){
    		  return ChartUtil.createXmlSeriesChart();
    	  }
        return chart;  
    } 
    @RequestMapping("/save")
    public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr){
       // String code =  service.save(form);
        /*if(code.equals("000")){
            attr.addFlashAttribute("name", form.getName());  
            attr.addFlashAttribute("success", "添加成功!");
            return "redirect:/index";
        }else{
            attr.addAttribute("projectName", form.getProjectName());  
            attr.addAttribute("enviroment", form.getEnviroment());  
            attr.addFlashAttribute("msg", "添加出错!错误码为："+rsp.getCode().getCode()+",错误为："+rsp.getCode().getName());
            return "redirect:/maintenance/toAddConfigCenter";
        }*/
		return "redirect:/index";
    }

}
/*
 * 方式一：使用ModelAndView return new ModelAndView("redirect:/toList");
 * 这样可以重定向到toList这个方法 方式二：返回String return "redirect:/ toList "; 方式一：自己手动拼接url
 * 
 * new ModelAndView("redirect:/toList？param1="+value1+"&param2="+value2);
 * 这样有个弊端，就是传中文可能会有乱码问题。
 * 
 * 方式二：用RedirectAttributes，这个是发现的一个比较好用的一个类
 * 这里用它的addAttribute方法，这个实际上重定向过去以后你看url，是它自动给你拼了你的url。 使用方法：
 *  ModelAndView mv = new ModelAndView("/user/save/result");//默认为forward模式  
    ModelAndView mv = new ModelAndView("redirect:/user/save/result");//redirect模式  
 * attr.addAttribute("param", value); return "redirect:/namespace/toController";
 * 这样在toController这个方法中就可以通过获得参数的方式获得这个参数，再传递到页面。过去的url还是和方式一一样的。
 */
